const PositionDao = require("../dao/position-dao.js")

const PositionService = {
	add(req, res, next) {
		// 从请求中获取待添加的数据
		const {company, position, salary, address} = req.body;

		// 如果有上传文件
		let logo = "";
		if (req.file) {
			logo = "/imgs/upload/" + req.file.filename;
		}

		// 保存到数据库中
		PositionDao.save({company, position, salary, address, logo})
							.then(data=>{
								res.json({
								  "res_code": 1,
								  "res_error": "",
								  "res_body": {
								    "status": 200,
								    "ret": {
								      "code": 1,
								      "message": "添加成功",
								      "data": data
								    }
								  }
								})
							})
							.catch(err=>{
								res.send({
								  "res_code": 0,
								  "res_error": err,
								  "res_body": {}
								});
							});
	},
	find(req, res, next) {

	},
	update(req, res, next) {
		// 获取待更新数据
		const {_id, company, position, salary, address} = req.body;
		let logo;
		if (req.file)
			logo = "/imgs/upload/" + req.file.filename;
		PositionDao.update({_id, company, position, salary, address, logo})
							.then(data=>{
								res.json({
								  "res_code": 1,
								  "res_error": "",
								  "res_body": {
								    "status": 200,
								    "ret": {
								      "code": 1,
								      "message": "更新成功",
								      "data": {_id, company, position, salary, address, logo}
								    }
								  }
								})
							})
							.catch(err=>{
								res.send({
								  "res_code": 0,
								  "res_error": err,
								  "res_body": {}
								});
							});
	},
	delete(req, res, next) {

	}
};

module.exports = PositionService;
